package controller.servlet;

/**
 * @author André
 *
 */
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Film;
import service.DatentraegerService;
import service.FilmService;
import service.Service;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.MessageHolder;
import dao.util.DaoContextEnum;

/**
 * Servlet implementation class ProdDetailServlet
 */
@SuppressWarnings("serial")
public class FilmDetailServlet extends HttpServlet {

	Film actualFilm = null;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		MessageHolder holder = new MessageHolder();
		try {
			request.setAttribute("datentraegers",
					((DatentraegerService) ServiceFactory.getServiceForName(
							"DatentraegerService", DaoContextEnum.MYSQL))
							.getDatentraegerInfos());

			Service<Film> service = (FilmService) ServiceFactory
					.getServiceForName("FilmService", DaoContextEnum.MYSQL);
			if ((request.getParameter("film") == null || (request
					.getParameter("film")).equals("0"))
					&& (request.getAttribute("film") == null || request
							.getAttribute("film").equals("0"))) {
				actualFilm = new Film("new Film", "new Hauptdarsteller",
						"new Genre", 1);
			} else {
				String filmId = request.getParameter("film");
				if (filmId == null) {
					filmId = (String) request.getAttribute("film");
				}
				actualFilm = (((FilmService) service).getFilmById(filmId));
			}
			if (actualFilm != null && actualFilm.getName() != null) {
				request.setAttribute("actualFilm", actualFilm);
				request.getRequestDispatcher("filmDetail.jsp").forward(request,
						response);
				return;
			}
		} catch (ServiceException e) {
			holder.setMessage(e.getMessage());
			request.setAttribute("actualFilm", actualFilm);
			request.setAttribute("message", holder);
			request.getRequestDispatcher("filmDetail.jsp").forward(request,
					response);
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		MessageHolder holder = new MessageHolder();
		try {
			FilmService service = ((FilmService) ServiceFactory
					.getServiceForName("FilmService", DaoContextEnum.MYSQL));
			String id = request.getParameter("id");
			if (id == null || id.equals("null") || id.isEmpty()) {
				actualFilm = null;
			} else {
				actualFilm = service.getFilmById(id);
			}

			if (request.getParameter("filmNameOk") != null) {
				actualFilm.setName(request.getParameter("filmName"));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("hptDarstOk") != null) {
				actualFilm.setHauptdarsteller(request
						.getParameter("hauptdarsteller"));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("genreOk") != null) {
				actualFilm.setGenre(request.getParameter("genre"));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("laengeOk") != null) {
				actualFilm.setLaenge(Integer.parseInt(request
						.getParameter("laenge")));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("filmDatOk") != null) {
				actualFilm.setDatentraegerId(request.getParameter("filmDat"));
				actualFilm
						.setDatentraeger(((DatentraegerService) ServiceFactory
								.getServiceForName("DatentraegerService",
										DaoContextEnum.MYSQL))
								.getDatentraegerById(actualFilm
										.getDatentraegerID().toString()));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("allOk") != null) {
				actualFilm.setName(request.getParameter("filmName"));
				actualFilm.setHauptdarsteller(request
						.getParameter("hauptdarsteller"));
				actualFilm.setGenre(request.getParameter("genre"));
				actualFilm.setLaenge(Integer.parseInt(request
						.getParameter("laenge")));
				actualFilm.setDatentraegerId(request.getParameter("filmDat"));
				actualFilm
						.setDatentraeger(((DatentraegerService) ServiceFactory
								.getServiceForName("DatentraegerService",
										DaoContextEnum.MYSQL))
								.getDatentraegerById(actualFilm
										.getDatentraegerID().toString()));
				service.saveToDatabase(actualFilm);
				request.setAttribute("film", id);

			} else if (request.getParameter("save") != null) {
				actualFilm = new Film(request.getParameter("filmName"));
				actualFilm.setHauptdarsteller(request
						.getParameter("hauptdarsteller"));
				actualFilm.setGenre(request.getParameter("genre"));
				actualFilm.setLaenge(Integer.parseInt(request
						.getParameter("laenge")));
				actualFilm.setDatentraegerId(request.getParameter("filmDat"));
				actualFilm
						.setDatentraeger(((DatentraegerService) ServiceFactory
								.getServiceForName("DatentraegerService",
										DaoContextEnum.MYSQL))
								.getDatentraegerById(actualFilm
										.getDatentraegerID().toString()));
				service.saveToDatabase(actualFilm);
				holder.setMessage("Film gespeichert!");
				response.sendRedirect("filmDetailServlet?film="
						+ actualFilm.getId().toString());
				return;

			} else if (request.getParameter("new") != null) {
				doGet(request, response);
				return;

			} else if (request.getParameter("clear") != null) {
				if (service.deleteFilm(actualFilm) == 1) {
					holder.setMessage("Film gelöscht");
					request.setAttribute("message", holder);
					doGet(request, response);
					return;
				}
				holder.setMessage("Film konnte nicht gelöscht werden");
				request.setAttribute("message", holder);
				doGet(request, response);
				return;

			} else {
				holder.setMessage("Sonderfehler!");
				request.setAttribute("message", holder);
				doGet(request, response);
				return;
			}

			request.getSession().setAttribute("actualFilm", actualFilm);

			doGet(request, response);

		} catch (Exception e) {
			holder.setMessage(e.getMessage());
			request.setAttribute("Datentraegers",
					((DatentraegerService) ServiceFactory.getServiceForName(
							"DatentraegerService", DaoContextEnum.MYSQL))
							.getDatentraegerInfos());
			request.setAttribute("actualFilm", actualFilm);
			request.setAttribute("message", holder);
			request.getRequestDispatcher("filmDetail.jsp").forward(request,
					response);
			return;
		}
	}

}
